<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>仪表盘</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        :root {
            --bg-primary: rgba(26, 26, 26, 0.45);
            --bg-secondary: #1a1a1a;
            --bg-tertiary: #2a2a2a;
            --accent-primary: #00d4ff;
            --accent-secondary: #ff6b6b;
            --text-primary: #ffffff;
            --text-secondary: #b0b0b0;
            --text-muted: #666666;
            --border-color: #333333;
            --shadow-color: rgba(0, 212, 255, 0.1);
            --gradient-primary: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            --gradient-secondary: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            --gradient-tertiary: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            --gradient-quaternary: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: var(--bg-primary);
            color: var(--text-primary);
            padding: 20px;
            min-height: 100vh;
        }
        
        .dashboard-container {
            max-width: 1600px;
            margin: 0 auto;
        }
        
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .stat-card {
            background: rgba(26, 26, 26, 0.85);
            border: 1px solid var(--border-color);
            border-radius: 16px;
            padding: 24px;
            backdrop-filter: blur(15px);
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            position: relative;
            overflow: hidden;
            min-height: 180px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
        
        .stat-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(135deg, rgba(0, 212, 255, 0.08) 0%, transparent 60%);
            opacity: 0;
            transition: opacity 0.3s ease;
        }
        
        .stat-card:hover::before {
            opacity: 1;
        }
        
        .stat-card:hover {
            transform: translateY(-8px) scale(1.02);
            border-color: var(--accent-primary);
            box-shadow: 0 20px 40px rgba(0, 212, 255, 0.15), 0 0 0 1px rgba(0, 212, 255, 0.1);
        }
        
        .stat-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 16px;
            position: relative;
            z-index: 2;
        }
        
        .stat-icon {
            width: 56px;
            height: 56px;
            border-radius: 14px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 1.6rem;
            color: white;
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.3);
            position: relative;
        }
        
        .stat-icon::after {
            content: '';
            position: absolute;
            top: 2px;
            left: 2px;
            right: 2px;
            bottom: 2px;
            border-radius: 12px;
            background: rgba(255, 255, 255, 0.1);
            opacity: 0;
            transition: opacity 0.3s ease;
        }
        
        .stat-card:hover .stat-icon::after {
            opacity: 1;
        }
        
        .stat-icon.primary {
            background: var(--gradient-primary);
        }
        
        .stat-icon.secondary {
            background: var(--gradient-secondary);
        }
        
        .stat-icon.tertiary {
            background: var(--gradient-tertiary);
        }
        
        .stat-icon.quaternary {
            background: var(--gradient-quaternary);
        }
        
        .stat-content {
            position: relative;
            z-index: 2;
            flex: 1;
        }
        
        .stat-value {
            font-size: 2.8rem;
            font-weight: 800;
            color: var(--text-primary);
            margin-bottom: 8px;
            line-height: 1;
            background: linear-gradient(135deg, #ffffff 0%, #e0e0e0 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .stat-label {
            color: var(--text-secondary);
            font-size: 0.95rem;
            font-weight: 500;
            margin-bottom: 12px;
            letter-spacing: 0.5px;
        }
        
        .stat-change {
            display: flex;
            align-items: center;
            font-size: 0.88rem;
            font-weight: 600;
            gap: 6px;
            padding: 6px 12px;
            border-radius: 20px;
            width: fit-content;
        }
        
        .stat-change.positive {
            color: #10b981;
            background: rgba(16, 185, 129, 0.1);
            border: 1px solid rgba(16, 185, 129, 0.2);
        }
        
        .stat-change.negative {
            color: #ef4444;
            background: rgba(239, 68, 68, 0.1);
            border: 1px solid rgba(239, 68, 68, 0.2);
        }
        
        .stat-change i {
            font-size: 0.8rem;
        }
        
        /* 响应式设计 */
        @media (max-width: 1000px) {
            .stats-grid {
                grid-template-columns: repeat(3, 1fr);
            }
        }
        
        @media (max-width: 768px) {
            .stats-grid {
                grid-template-columns: repeat(2, 1fr);
            }
        }
        
        @media (max-width: 480px) {
            .stats-grid {
                grid-template-columns: 1fr;
            }
            
            .dashboard-container {
                padding: 0 10px;
            }
            
            .stat-card {
                padding: 20px;
                min-height: 160px;
            }
            
            .stat-value {
                font-size: 2.4rem;
            }
        }
        
        .chart-container {
            background: rgba(26, 26, 26, 0.8);
            border: 1px solid var(--border-color);
            border-radius: 15px;
            padding: 25px;
            backdrop-filter: blur(10px);
            margin-bottom: 30px;
        }
        
        .chart-header {
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-bottom: 20px;
        }
        
        .chart-title {
            font-size: 1.3rem;
            font-weight: 600;
            color: var(--text-primary);
        }
        
        .chart-placeholder {
            height: 300px;
            background: rgba(42, 42, 42, 0.5);
            border-radius: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: var(--text-muted);
            font-size: 1.1rem;
        }
        
        .quick-actions {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
        }
        
        .action-btn {
            background: rgba(26, 26, 26, 0.8);
            border: 1px solid var(--border-color);
            border-radius: 10px;
            padding: 15px 20px;
            color: var(--text-primary);
            text-decoration: none;
            display: flex;
            align-items: center;
            gap: 10px;
            transition: all 0.3s ease;
            cursor: pointer;
        }
        
        .action-btn:hover {
            background: rgba(42, 42, 42, 0.8);
            border-color: var(--accent-primary);
            transform: translateY(-2px);
        }
        
        .loading {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            padding: 40px;
            color: var(--text-secondary);
            grid-column: 1 / -1;
        }
        
        .loading-spinner {
            width: 40px;
            height: 40px;
            border: 3px solid var(--border-color);
            border-top: 3px solid var(--accent-primary);
            border-radius: 50%;
            animation: spin 1s linear infinite;
            margin-bottom: 15px;
        }
        
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
    </style>
</head>
<body>
    <div class="dashboard-container">
        <div class="stats-grid" id="stats-grid">
            <div class="loading">
                <div class="loading-spinner"></div>
                <p>正在加载统计数据...</p>
            </div>
        </div>
        
        <div class="chart-container">
            <div class="chart-header">
                <h3 class="chart-title">系统性能趋势</h3>
                <select class="chart-filter">
                    <option value="24h">最近24小时</option>
                    <option value="7d">最近7天</option>
                    <option value="30d">最近30天</option>
                </select>
            </div>
            <div class="chart-placeholder">
                📊 性能图表将在此显示
            </div>
        </div>
        
        <div class="quick-actions">
            <a href="#" class="action-btn" onclick="parent.switchModule('queue')">
                <i class="fas fa-list"></i>
                <span>队列管理</span>
            </a>
            <a href="#" class="action-btn" onclick="parent.switchModule('service')">
                <i class="fas fa-network-wired"></i>
                <span>服务发现</span>
            </a>
            <a href="#" class="action-btn" onclick="parent.switchModule('config')">
                <i class="fas fa-cogs"></i>
                <span>配置中心</span>
            </a>
            <a href="#" class="action-btn" onclick="parent.switchModule('mcp')">
                <i class="fas fa-tools"></i>
                <span>MCP工具</span>
            </a>
        </div>
    </div>
    
    <script>
        // 获取统计数据
        async function loadDashboardStats() {
            try {
                // 同时获取原有统计数据和队列指标数据
                const [dashboardResponse, queueMetricsResponse] = await Promise.all([
                    fetch('/api/console/dashboard/stats'),
                    fetch('/api/console/queues/metrics')
                ]);
                
                const dashboardData = await dashboardResponse.json();
                const queueMetricsData = await queueMetricsResponse.json();
                
                if (dashboardData.success && queueMetricsData.success) {
                    // 合并数据
                    const combinedStats = {
                        ...dashboardData.data,
                        unifiedMessages: queueMetricsData.data.UNIFIED.totalMessages || 0,
                        streamMessages: queueMetricsData.data.STREAM_UNIFIED.totalMessages || 0
                    };
                    renderStats(combinedStats);
                } else {
                    showError('加载统计数据失败');
                }
            } catch (error) {
                console.error('加载统计数据失败:', error);
                renderMockStats();
            }
        }
        
        // 渲染统计数据
        function renderStats(stats) {
            const statsGrid = document.getElementById('stats-grid');
            statsGrid.innerHTML = `
                <div class="stat-card">
                    <div class="stat-header">
                        <div class="stat-icon primary">
                            <i class="fas fa-bolt"></i>
                        </div>
                    </div>
                    <div class="stat-content">
                        <div class="stat-value">${stats.unifiedMessages || 0}</div>
                        <div class="stat-label">UNIFIED消息</div>
                        <div class="stat-change positive">
                            <i class="fas fa-flash"></i>
                            <span>瞬时消息</span>
                        </div>
                    </div>
                </div>
                
                <div class="stat-card">
                    <div class="stat-header">
                        <div class="stat-icon secondary">
                            <i class="fas fa-stream"></i>
                        </div>
                    </div>
                    <div class="stat-content">
                        <div class="stat-value">${stats.streamMessages || 0}</div>
                        <div class="stat-label">STREAM消息</div>
                        <div class="stat-change positive">
                            <i class="fas fa-water"></i>
                            <span>流消息</span>
                        </div>
                    </div>
                </div>
                
                <div class="stat-card">
                    <div class="stat-header">
                        <div class="stat-icon tertiary">
                            <i class="fas fa-server"></i>
                        </div>
                    </div>
                    <div class="stat-content">
                        <div class="stat-value">${stats.serviceCount || 0}</div>
                        <div class="stat-label">服务实例</div>
                        <div class="stat-change positive">
                            <i class="fas fa-arrow-up"></i>
                            <span>+${stats.serviceGrowth || 0}%</span>
                        </div>
                    </div>
                </div>
                
                <div class="stat-card">
                    <div class="stat-header">
                        <div class="stat-icon quaternary">
                            <i class="fas fa-cogs"></i>
                        </div>
                    </div>
                    <div class="stat-content">
                        <div class="stat-value">${stats.configCount || 0}</div>
                        <div class="stat-label">配置项</div>
                        <div class="stat-change positive">
                            <i class="fas fa-arrow-up"></i>
                            <span>+${stats.configGrowth || 0}%</span>
                        </div>
                    </div>
                </div>
                
                <div class="stat-card">
                    <div class="stat-header">
                        <div class="stat-icon primary">
                            <i class="fas fa-tools"></i>
                        </div>
                    </div>
                    <div class="stat-content">
                        <div class="stat-value">${stats.toolCount || 0}</div>
                        <div class="stat-label">MCP工具</div>
                        <div class="stat-change positive">
                            <i class="fas fa-arrow-up"></i>
                            <span>+${stats.toolGrowth || 0}%</span>
                        </div>
                    </div>
                </div>
            `;
        }
        
        // 渲染模拟数据
        function renderMockStats() {
            const mockStats = {
                unifiedMessages: 156,
                streamMessages: 89,
                serviceCount: 5,
                serviceGrowth: 12,
                configCount: 28,
                configGrowth: 5,
                toolCount: 7,
                toolGrowth: 15
            };
            renderStats(mockStats);
        }
        
        // 显示错误信息
        function showError(message) {
            const statsGrid = document.getElementById('stats-grid');
            statsGrid.innerHTML = `
                <div class="stat-card" style="grid-column: 1 / -1;">
                    <div class="stat-header">
                        <div class="stat-icon secondary">
                            <i class="fas fa-exclamation-triangle"></i>
                        </div>
                    </div>
                    <div class="stat-content">
                        <div class="stat-label">${message}</div>
                        <button class="action-btn" onclick="loadDashboardStats()" style="margin-top: 15px; width: auto;">
                            <i class="fas fa-redo"></i>
                            <span>重新加载</span>
                        </button>
                    </div>
                </div>
            `;
        }
        
        // 页面加载完成后初始化
        document.addEventListener('DOMContentLoaded', function() {
            loadDashboardStats();
        });
    </script>
</body>
</html>